package com.micookie.v2ex.model;

import com.micookie.v2ex.api.HttpRequest;
import com.micookie.v2ex.api.inf.IGetDataCallback;
import com.micookie.v2ex.model.bean.Topic;
import com.micookie.v2ex.model.bean.TopicDetail;
import com.micookie.v2ex.utils.ParseUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Administrator on 2016-11-21.
 */

public class TopicModel {

    public void getTopicList(String url, final IGetDataCallback<List<Topic>> callback) {
        if (url == null || url.isEmpty()) {
            callback.onFailed("Url错误");
            return;
        }
        HttpRequest.getInstance().get(url, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String body = response.body().string();
                List<Topic> topicList = ParseUtils.parseTopicList(body);
                callback.onSuccess(topicList);
            }

            @Override
            public void onFailure(Call call, IOException e) {
                callback.onFailed(e.getMessage());
            }
        });
    }

    public void getTopicDetail(String url, final IGetDataCallback<TopicDetail> callback) {
        HttpRequest.getInstance().get(url, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String body = response.body().string();
                TopicDetail topicDetail = ParseUtils.parseTopicDetail(body);
                callback.onSuccess(topicDetail);
            }

            @Override
            public void onFailure(Call call, IOException e) {
                callback.onFailed(e.getMessage());
            }

        });
    }


}
